/*  
 *  ------ [ZB_04] - receive packet -------- 
 *  
 *  Explanation: This example shows hot to receive packets with XBee
 *  modules 
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */
 
#include <MakeSureXBeeZB.h>

#include <MakeSureFrame.h>


uint8_t network_address[2];


void setup()
{  
  // init Serial port
  Serial.begin(9600);
  Serial.print("ZB_04 example Receive");

  //////////////////////////
  // 1. init XBee
  //////////////////////////
  xbeeZB.ON();
  
  delay(3000);
  
    
  //////////////////////////
  // 2. check XBee's network parameters
  //////////////////////////
  checkNetworkParams();
  
}

void loop()
{ 
  //////////////////////////
  // 3. receive packets 
  //////////////////////////
  
  // check available data in RX buffer
  if( xbeeZB.available()>0 ) 
  {
	  Serial.print("Data Available...");
    // read a packet when XBee has noticed it to us
    xbeeZB.treatData(); 
    
    // check RX flag after 'treatData'
    if( !xbeeZB.error_RX ) 
    {
      // read available packets
      while( xbeeZB.pos>0 )
      {
        // Available information in 'xbeeZB.packet_finished' structure
        // HERE it should be introduced the User's packet treatment        
        // For example: show DATA field:
             
        Serial.print("Data: ");                    
        for(int i=0;i<xbeeZB.packet_finished[xbeeZB.pos-1]->data_length;i++)          
        { 
          // Print data payload
          Serial.print(xbeeZB.packet_finished[xbeeZB.pos-1]->data[i],HEX);          
        }
        Serial.println("");

        //free memory
        free(xbeeZB.packet_finished[xbeeZB.pos-1]); 
        
        //free pointer
        xbeeZB.packet_finished[xbeeZB.pos-1]=NULL; 
        
        //Decrement the received packet counter
        xbeeZB.pos--; 
      }
    }
  }
} 



/*******************************************
 *
 *  checkNetworkParams - Check operating
 *  network parameters in the XBee module
 *
 *******************************************/
void checkNetworkParams()
{
	xbeeZB.getOwnMac();
	
	 Serial.println("Source MAC ID: ");
     Serial.print(xbeeZB.sourceMacHigh[0],HEX);
     Serial.print(xbeeZB.sourceMacHigh[1],HEX);
     Serial.print(xbeeZB.sourceMacHigh[2],HEX);
     Serial.print(xbeeZB.sourceMacHigh[3],HEX);
     Serial.print(xbeeZB.sourceMacLow[0],HEX);
     Serial.print(xbeeZB.sourceMacLow[1],HEX);
     Serial.print(xbeeZB.sourceMacLow[2],HEX);
     Serial.print(xbeeZB.sourceMacLow[3],HEX);
     Serial.println();     
  // 1. get operating 64-b PAN ID
  xbeeZB.getOperating64PAN();

  // 2. wait for association indication
  xbeeZB.getAssociationIndication();
 
  while( xbeeZB.associationIndication != 0 )
  { 
    delay(2000);
    
    // get operating 64-b PAN ID
    xbeeZB.getOperating64PAN();

    Serial.print("operating 64-b PAN ID: ");
    Serial.print(xbeeZB.operating64PAN[0],HEX);
    Serial.print(xbeeZB.operating64PAN[1],HEX);
    Serial.print(xbeeZB.operating64PAN[2],HEX);
    Serial.print(xbeeZB.operating64PAN[3],HEX);
    Serial.print(xbeeZB.operating64PAN[4],HEX);
    Serial.print(xbeeZB.operating64PAN[5],HEX);
    Serial.print(xbeeZB.operating64PAN[6],HEX);
    Serial.print(xbeeZB.operating64PAN[7],HEX);
    Serial.println();     
    
    xbeeZB.getAssociationIndication();
  }

  Serial.println("\nJoined a network!");

  // 3. get network parameters 
  xbeeZB.getOperating16PAN();
  xbeeZB.getOperating64PAN();
  xbeeZB.getChannel();

  Serial.println("operating 16-b PAN ID: ");
  Serial.print(xbeeZB.operating16PAN[0],HEX);
  Serial.print(xbeeZB.operating16PAN[1],HEX);
  Serial.println();

  Serial.println("operating 64-b PAN ID: ");
  Serial.print(xbeeZB.operating64PAN[0],HEX);
  Serial.print(xbeeZB.operating64PAN[1],HEX);
  Serial.print(xbeeZB.operating64PAN[2],HEX);
  Serial.print(xbeeZB.operating64PAN[3],HEX);
  Serial.print(xbeeZB.operating64PAN[4],HEX);
  Serial.print(xbeeZB.operating64PAN[5],HEX);
  Serial.print(xbeeZB.operating64PAN[6],HEX);
  Serial.print(xbeeZB.operating64PAN[7],HEX);
  Serial.println();

  Serial.print("channel: ");
  Serial.print(xbeeZB.channel,HEX);
  Serial.println();

}
